package cc.ewell.esb.authenticating.util;

import cc.ewell.esb.authenticating.Call;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.net.URLCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HttpURLConnectionUtil {
    private static Logger log = LoggerFactory.getLogger(HttpURLConnectionUtil.class);

    public String get(String path) {
        try {
            URL url = new URL(path.trim());

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            if (200 == urlConnection.getResponseCode()) {

                InputStream is = urlConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while (-1 != (len = is.read(buffer))) {
                    baos.write(buffer, 0, len);
                    baos.flush();
                }
                return baos.toString("utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    public String post(String path, String post) {
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        try {
            url = new URL(path);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(2000);

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());

            printWriter.write(post);

            printWriter.flush();
            printWriter.close();

            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte[] arr = new byte[51200];
            int len;
            while ((len = bis.read(arr)) != -1) {
                bos.write(arr, 0, len);
                bos.flush();
            }
            bos.close();
            bis.close();
            return bos.toString("utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpURLConnection.disconnect();
        }
        return null;
    }

    public String httpPost(String path, String post, Integer timeOut) throws IOException {
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        try {
            url = new URL(path);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(timeOut);

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());

            printWriter.write(post);

            printWriter.flush();
            printWriter.close();

            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte[] arr = new byte[51200];
            int len;
            while ((len = bis.read(arr)) != -1) {
                bos.write(arr, 0, len);
                bos.flush();
            }
            bos.close();
            bis.close();
            return bos.toString("utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            throw (e);
        } finally {
            httpURLConnection.disconnect();
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String paramXml = Call.getParam(null);
        HttpURLConnectionUtil util = new HttpURLConnectionUtil();
        long beginTime = System.currentTimeMillis();
        String url = "http://www.esb-auth.com:7003/integration-esb-authenticating-server/integration/interfaces/reqreply.do";
        String param = "paramXml=" + paramXml + "&method=" + "authenticateReqResp";


        System.out.println(util.post(url, param));
        System.out.println("本次调用耗时" + (System.currentTimeMillis() - beginTime) + "毫秒 ");
    }


    public String post(String url, String methodName, String paramXml) {
        URLCodec urlCodec = new URLCodec();
        try {
            String post = "paramXml=" + urlCodec.encode(paramXml, "UTF-8") + "&method=" + urlCodec.encode(methodName, "UTF-8");

            return post(url, post);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String post(String url, String methodName, String paramXml, String traceId) {
        URLCodec urlCodec = new URLCodec();
        try {
            String post = "paramXml=" + urlCodec.encode(paramXml, "UTF-8") +
                    "&method=" + urlCodec.encode(methodName, "UTF-8") +
                    "&reqTraceId=" + urlCodec.encode(traceId, "UTF-8") +
                    "&reqTime=" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
            int retryCount = 0;
            String res = null;
            do {
                try {
                    res = httpPost(url, post, 600);
                    break;
                } catch (Exception e) {
                    retryCount++;
                    log.warn("traceId: {}|httpRetry: {}|errorMsg: {}", traceId, retryCount, e.getMessage(), e);
                    if (retryCount > 3) {
                        throw e;
                    }
                }
            } while (true);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("traceId: {}|errorMsg: {}", traceId, e.getMessage(), e);
            return null;
        }
    }
}


/* Location:              C:\Users\taoo6\Desktop\integrationEsbAuthenticating-client-1.0.jar!\cc\ewell\esb\authenticatin\\util\HttpURLConnectionUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.1.3
 */